package com.smarttiger.zero.logic.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.smarttiger.zero.admin.dao.SubscribeDao;
import com.smarttiger.zero.admin.entity.SubscribeModel;
import com.smarttiger.zero.admin.util.Constant;
import com.smarttiger.zero.logic.BaseLogic;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouzhengwei on 2021/10/25.
 * 调接口发消息专用
 */
@Component
public class SendMessageDataLogic extends BaseLogic {

    @Autowired
    SubscribeDao subscribeDao;

    @Override
    public void logic() {

        Map paramValue = (Map) getParamValue(Constant.INTERFACECURRENT);
        Object messageCode = paramValue.get("messageCode");

        if (StringUtils.isEmpty((String) messageCode)) {
            setResult("messageCode参数不能为空");
            return;
        }
        Object data = paramValue.get("data");
        if (data != null) {
            String dataStr = JSON.toJSONString(data);
            List<SubscribeModel> subscribeModels = subscribeDao.findAllByMessageCode(messageCode.toString());
            for (SubscribeModel subscribeModel : subscribeModels) {
                ThreadUtil.execute(new ExcuteMessage(dataStr,subscribeModel));
            }
        }else{
            setResult("data参数不能为空");
            return;
        }
        setResult("调用完成");
    }

    class ExcuteMessage extends Thread {
        private String dataStr;
        private SubscribeModel subscribeModel;

        public ExcuteMessage(String dataStr, SubscribeModel subscribeModel) {
            this.dataStr = dataStr;
            this.subscribeModel = subscribeModel;
        }

        @Override
        public void run() {
            String s = subscribeModel.getmType();
            String url = subscribeModel.getUrl();
            String arg = subscribeModel.getArg();
            if ("http".equalsIgnoreCase(s)) {
                HashMap<String, Object> argMap = new HashMap<>();
                argMap.put(arg, dataStr);
                for (int i = 0; i < 3; i++) {
                    String post = HttpUtil.post(url, argMap, 5000);
                    HashMap hashMap = JSON.parseObject(post, HashMap.class);
                    if ("1".equals(hashMap.get("state"))) {
                        break;
                    }
                }
            } else if ("ws".equalsIgnoreCase(s)) {
                JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
                Client client = dcf.createClient(url);
                Object[] objects = new Object[0];
                for (int i = 0; i < 3; i++) {
                    try {
                        objects = client.invoke(arg, dataStr);
                        Object object = objects[0];
                        HashMap hashMap = JSON.parseObject(object.toString(), HashMap.class);
                        if ("1".equals(hashMap.get("state"))) {
                            break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        public String getDataStr() {
            return dataStr;
        }

        public void setDataStr(String dataStr) {
            this.dataStr = dataStr;
        }

        public SubscribeModel getSubscribeModel() {
            return subscribeModel;
        }

        public void setSubscribeModel(SubscribeModel subscribeModel) {
            this.subscribeModel = subscribeModel;
        }
    }
}
